<?php
set_time_limit(0);
error_reporting(0);

include './vendor/autoload.php';
include './func/func.php';
include './func/code.php';


// 搜索列表备案
function run()
{
	echo '开始:'.PHP_EOL;
	$start = microtime(true);
	$links = input_data();
	foreach ($links as $key => $item) {
		list($title, $domain, $icp) = $item;
		echo "{$domain} \t查询中...".PHP_EOL;
		$info = valid($domain);

		$info['link'] = "{$title},{$domain},{$icp}";
		output_data($info);
	}
	$end = microtime(true);
	echo 'Done!'.PHP_EOL;
	echo 'use time:'.($end-$start).PHP_EOL;
}
// run();


// 搜索备案号
function valid($domain = 'baidu.com')
{
	// 获取 cookie
	$base = 'http://www.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_showPage.action';
	$html = http($base);

	// 获取验证码
	$code_url  = 'http://www.miitbeian.gov.cn/getVerifyCode?';
	// 校对验证码
	$valid_url = 'http://www.miitbeian.gov.cn/common/validate/validCode.action';
	$valid = false;
	$text  = '';


	// 识别验证码直到正确
	do {
		// 获取图片
		$file = down($code_url.mt_rand(5, 100));
		$path = __DIR__.'/images/color.png';
		file_put_contents($path, $file);
		$file = @imagecreatefromstring($file);

		sleep(3);
		if (!is_resource($file)) {
			echo 'valid code image download fail!!!'.PHP_EOL;
			continue;
		}


		// 处理图片
		$img  = image_one($file);
		$path = __DIR__.'/images/code.png';
		file_exists($path) && unlink($path);
		imagepng($img, $path);


		// 识别图片 code fontyp num eng
		$text = (new TesseractOCR($path))->lang('eng')->psm(7)->run();
		$text = code_replace($text);
		if (strlen($text) != 6 || $text == false) {
			continue;
		}


		// 验证码校验
		$res   = http($valid_url, ['validateValue'=>$text]);
		$valid = json_decode($res, true);
		$valid = $valid['result'];

	} while (!$valid);


	// 执行查询
	$exec =	'http://www.miitbeian.gov.cn/icp/publish/query/icpMemoInfo_searchExecute.action';
	$args = [
		'siteName'=>'',
		'condition'=>'1',
		'siteDomain'=>$domain,
		'siteUrl'=>'',
		'mainLicense'=>'',
		'siteIp'=>'',
		'unitName'=>'',
		'mainUnitNature'=>-1,
		'certType'=>-1,
		'mainUnitCertNo'=>'',
		'verifyCode'=>$text
	];

	// 返回结果
	$html = http($exec, $args);
	$html = iconv('GBK', 'UTF-8', $html);
	return get_info($html);
}



// 去线
function test3($filename)
{
	// 读入图片
	$ext = pathinfo($filename, PATHINFO_EXTENSION);
	$ext = strtolower($ext);
	if ($ext == 'png') {
		$gd_resource = @imagecreatefrompng($filename);
	} elseif ($ext == 'jpeg' || $ext == 'jpg') {
		$gd_resource = @imagecreatefromjpeg($filename);
	} else {
		die('file format error');
	}


}


// 去掉指定目录的验证码的干扰线
function test4()
{
	$base = __DIR__.'/balck';
	$handler = opendir($base);
	while( ($filename = readdir($handler)) !== false ) {
		if($filename[0] === '.') {
			continue;
		}
		$img  = test3($base.'/'.$filename);
		$path = '/Users/bin/Pictures/back/'.strstr($filename, '.', true).'.png';
		imagepng($img, $path);
		// exit();
	}
}
test4();


// 下载图片
function test5($number = 150)
{
	$i = 1;
	$code_url  = 'http://www.miitbeian.gov.cn/getVerifyCode?';
	while ($i <= $number) {

		// 下载
		$file = down($code_url.mt_rand(5, 100));
		$file = @imagecreatefromstring($file);

		// 是否有效
		sleep(2);
		if (!is_resource($file)) {
			echo 'valid code image download fail!!!'.PHP_EOL;
			continue;
		}

		// 保存
		$path = sprintf(__DIR__.'/temp/code%03d.png', $i++);
		imagepng($file, $path);
	}
	echo 'test5 Done!'.PHP_EOL;
}
// test5();
